<!-- ERP 物料调拨 -->
<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item  prop="no">
        <el-input
          v-model="queryParams.no"
          placeholder="请输入调拨单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
   <el-form-item  prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择单据状态" clearable class="!w-240px">
          <el-option label="待提交" :value="0" />
          <el-option label="待审核" :value="1" />
          <el-option label="已审核" :value="2" />
        </el-select>
      </el-form-item>
        <el-form-item  prop="fromDepartmentName">
        <el-input
          v-model="queryParams.fromDepartmentName"
          placeholder="请输入调出部门/调入部门"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
       <el-form-item  prop="updateTime">
        <el-date-picker
          v-model="queryParams.updateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="更新开始日期"
          end-placeholder="更新结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
        <el-form-item  prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable class="!w-240px">
          <el-option label="审核中" :value="0" />
          <el-option label="审核不通过" :value="1" />
          <el-option label="审核通过" :value="2" />
        </el-select>
      </el-form-item>
        <el-form-item  prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入物料备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
       <el-form-item  prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入物料编码/名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-row :gutter="10" class="flex-justify-between w-full" style="margin-bottom: 16px">
      <div class="flex">
        <el-col :span="1.5">
          <div>
            <el-button
              type="primary"
              plain
              @click="openForm('create')"
              v-hasPermi="['erp:stock-check:create']"
            >
              新增
            </el-button>
          </div>
        </el-col>
        <el-col :span="1.5">
          <div>
            <el-button
              type="default"
              plain
              :disabled="!selectedRow"
              @click="handleSubmit"
              :loading="exportLoading"
              v-hasPermi="['system:tenant:export']"
            >
              提交
            </el-button>
          </div>
        </el-col>
        <el-col :span="1.5">
          <div>
            <el-button
              type="default"
              plain
              :disabled="!selectedRow"
              @click="handleAuditStatus()"
              v-hasPermi="['system:tenant:export']"
            >
              审核
            </el-button>
          </div>
        </el-col>
        <el-col :span="1.5">
          <div>
            <el-button type="default" plain :disabled="!selectedRow" @click="handleAntiAudit()">
              反审核
            </el-button>
          </div>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="default"
            plain
            @click="handleDelete"
            :disabled="!selectedRow"
            v-hasPermi="['system:tenant:export']"
          >
            删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <div>
            <el-button
              type="default"
              plain
              @click="handleExport"
              :loading="exportLoading"
              v-hasPermi="['system:tenant:export']"
            >
              导出
            </el-button>
          </div>
        </el-col>
      </div>
      <div class="flex items-center">
        <el-col :span="1.5">
          <span class="text-[14px] mr-[4px] color-#606266">整单</span>
          <el-switch v-model="order" size="middle" />
        </el-col>
        <el-col :span="10.1">
          <div style="text-align: right">
            <el-button type="default" plain @click="btnLie">
              <img key="1" alt="" width="25px" height="20px" src="@/assets/svgs/column.svg" />
              列表字段
            </el-button>
          </div>
        </el-col>
      </div>
    </el-row>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      @selection-change="handleSelectionChange"
      :show-overflow-tooltip="true"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="调拨单号" align="center" prop="no" />
      <el-table-column label="单据状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_STOCK_RECORD_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.ERP_AUDIT_STATUS" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="productCode" />
      <el-table-column label="物料名称" align="center" prop="productName" />
      <el-table-column label="基本数量" align="center" prop="baseQuantity" />
      <el-table-column label="调出仓库" align="center" prop="fromWarehouseName" />
      <el-table-column label="调出库区" align="center" prop="fromAreaName" />
      <el-table-column label="调出库位" align="center" prop="fromGridName" />
      <el-table-column label="调出批号" align="center" prop="fromBatchNo" />
      <el-table-column label="调入仓库" align="center" prop="toWarehouseName" />
      <el-table-column label="调入库区" align="center" prop="toAreaName" />
      <el-table-column label="调入库位" align="center" prop="toGridName" />
      <el-table-column label="物料分类" align="center" prop="categoryName" />
      <el-table-column label="物料规格" align="center" prop="standard" />
      <el-table-column label="物料型号" align="center" prop="model" />
      <el-table-column label="库存单位" align="center" prop="unitName" />
      <el-table-column label="行备注" align="center" prop="remarkLine" />
      <el-table-column label="调出部门" align="center" prop="fromDepartmentName" />
      <el-table-column label="调入部门" align="center" prop="toDepartmentName" />
      <el-table-column label="制单人" align="center" prop="creator" />
      <el-table-column
        label="制单日期"
        align="center"
        prop="createDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="最后更新人" align="center" prop="updateBy" />
      <el-table-column
        label="最后更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
      />
      <el-table-column label="操作" align="center" fixed="right" width="400">
        <template #default="scope">
            <el-button
            link
            type="primary"
             @click="openForm('copy',scope.row.id)"
            v-hasPermi="['erp:product:update']"
          >
            复制
          </el-button>  <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:product:update']"
          >
            编辑
          </el-button>
            <el-button
            link
            type="primary"
            @click="submitForm('update', scope.row.id)"
            v-hasPermi="['erp:product:update']"
          >
            提交
          </el-button>
            <el-button
            link
            type="primary"
            @click="openForm('detail', scope.row.id)"
            v-hasPermi="['erp:product:update']"
          >

            详情
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:product:delete']"
          >
            删除
          </el-button>
           <el-button
            link
            type="primary"
            @click="print()"
            v-hasPermi="['erp:product:update']"
          >

            打印
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <MaterialForm ref="formRef" @success="getList" />
   <el-dialog
    v-model="dialogVisible"
    title="自定义字段"
    width="50%"
    :before-close="handleClose"
  >
    <div class="content">
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.image"  label="图片" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.bianhao"  label="产品编号" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.name"  label="产品名称" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.count"  label="产品分类" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.feilei"  label="产品规格" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.shuxing"  label="产品属性" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.beizhu"  label="产品备注" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.zhuangtai"  label="产品使用状态" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei"  label="单位(基本单位)" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.beizhu2"  label="入库备注" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei2"  label="销售单价(基本单位)" size="large" /></div>
      <div class="content-list"><el-checkbox v-model="tempVisibleColumns.danwei3"  label="成本单价(基本单位)" size="large" /></div>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>

<MaterialForm ref="formRef" @success="getList" />

</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ProductApi, ProductVO } from '@/api/erp/product/product'
import { ProductCategoryApi, ProductCategoryVO } from '@/api/erp/product/category'
import MaterialForm from './MaterialForm.vue'
import { DICT_TYPE } from '@/utils/dict'
import StockList from '@/views/erp/logistics/warehouse/components/StockList.vue'
import { defaultProps, handleTree } from '@/utils/tree'
import { erpPriceTableColumnFormatter } from '@/utils'
const dialogVisible=ref(false)
const visibleColumns =ref({
  image:true,
  bianhao:true,
  name:true,
  count:true,
  feilei:true,
  shuxing:true,
  beizhu:true,
  zhuangtai:true,
  danwei:true,
  beizhu2:true,
  danwei2:true,
  danwei3:true
})
const order =ref()
const selectedRow=ref()
const tempVisibleColumns = ref({...visibleColumns.value})
/** ERP 产品列表 */
defineOptions({ name: 'ErpProduct' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ProductVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  auditStatus: undefined,
  status:undefined,
  fromDepartmentName:undefined,
  updateTime:undefined,
  remark:undefined,
  productCode:undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const categoryList = ref<ProductCategoryVO[]>([]) // 产品分类列表

/** 查询列表 */
// const getList = async () => {
//   loading.value = true
//   try {
//     const data = await ProductApi.getProductPage(queryParams)
//     list.value = data.list
//     total.value = data.total
//   } finally {
//     loading.value = false
//   }
// }

const getList = async () => {
  loading.value = true
  try {
    // 模拟延迟
    await new Promise(resolve => setTimeout(resolve, 500));
    
    // 使用模拟数据替代API调用
    list.value = [
      {
        id: 1,
        no: "DB2023071001", // 调拨单号
        status: 0, // 单据状态：0=待提交, 1=待审核, 2=已审核
        auditStatus: 0, // 审核状态：0=审核中, 1=审核不通过, 2=审核通过
        productCode: "MAT202307001", // 物料编码
        productName: "金属螺丝", // 物料名称
        baseQuantity: 500, // 基本数量
        fromWarehouseName: "原材料仓", // 调出仓库
        fromAreaName: "A区", // 调出库区
        fromGridName: "A01", // 调出库位
        fromBatchNo: "BATCH20230701", // 调出批号
        toWarehouseName: "生产车间仓", // 调入仓库
        toAreaName: "B区", // 调入库区
        toGridName: "B05", // 调入库位
        categoryName: "紧固件", // 物料分类
        standard: "M6×30mm", // 物料规格
        model: "304不锈钢", // 物料型号
        unitName: "个", // 库存单位
        remarkLine: "紧急生产需求", // 行备注
        fromDepartmentName: "仓储部", // 调出部门
        toDepartmentName: "生产部", // 调入部门
        creator: "张三", // 制单人
        createDate: "2023-07-10 10:15:22", // 制单日期
        remark: "优先处理项目X", // 备注
        createBy: "系统管理员", // 创建人
        createTime: "2023-07-10 10:15:22", // 创建时间
        updateBy: "张三", // 最后更新人
        updateTime: "2023-07-10 10:30:45" // 最后更新时间
      },
      {
        id: 2,
        no: "DB2023071002",
        status: 2, // 已审核
        auditStatus: 2, // 审核通过
        productCode: "MAT202307002",
        productName: "电子传感器",
        baseQuantity: 50,
        fromWarehouseName: "电子元件仓",
        fromAreaName: "C区",
        fromGridName: "C03",
        fromBatchNo: "BATCH20230702",
        toWarehouseName: "成品仓",
        toAreaName: "D区",
        toGridName: "D12",
        categoryName: "传感器类",
        standard: "XYZ-1000",
        model: "工业级",
        unitName: "件",
        remarkLine: "客户项目专用",
        fromDepartmentName: "采购部",
        toDepartmentName: "销售部",
        creator: "李四",
        createDate: "2023-07-10 14:20:18",
        remark: "客户特别定制款",
        createBy: "系统管理员",
        createTime: "2023-07-10 14:20:18",
        updateBy: "李四",
        updateTime: "2023-07-10 15:05:32"
      }
    ];
    
    total.value = list.value.length;
    
    // 实际项目中使用API调用
    // const data = await ProductApi.getProductPage(queryParams)
    // list.value = data.list
    // total.value = data.total
  } finally {
    loading.value = false
  }
}
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

const handleSubmit=()=>{

}
const btnLie = () => {
  tempVisibleColumns.value = { ...visibleColumns.value }
  dialogVisible.value = true
}
const handleClose=()=>{
dialogVisible.value = false
}
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ProductApi.deleteProduct(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const handleSelectionChange =()=>{

}
/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ProductApi.exportProduct(queryParams)
    download.excel(data, '产品.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  await getList()
  // 产品分类
  const categoryData = await ProductCategoryApi.getProductCategorySimpleList()
  categoryList.value = handleTree(categoryData, 'id', 'parentId')
})
</script>
